package com.hqully.labmanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqully.labmanage.common.enums.ResultEnum;
import com.hqully.labmanage.common.exception.DAOException;
import com.hqully.labmanage.common.exception.ResultException;
import com.hqully.labmanage.domain.dto.PageDTO;
import com.hqully.labmanage.domain.dto.StuInfoDTO;
import com.hqully.labmanage.domain.entity.StuInfo;
import com.hqully.labmanage.domain.vo.StuInfoTree;
import com.hqully.labmanage.mapper.StuInfoMapper;
import com.hqully.labmanage.service.StuInfoService;
import com.hqully.labmanage.service.impl.constant.MapperConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 针对数据库表[stuInfo]的操作Service
 * </p>
 *
 * @author hqully <br>
 * @version 1.0
 * @entity {@link StuInfo}
 * @date 2022-10-10 19:43:54 <br>
 */
@Slf4j
@Service
public class StuInfoServiceImpl implements StuInfoService {

    private static final String ROOT = "1";

    @Resource
    private StuInfoMapper stuInfoMapper;


    @Override
    public StuInfoTree getInfoTree(String code) {
        StuInfoTree infoTree = null;
        try {
            if (code != null) {
                infoTree = stuInfoMapper.getInfoTree(code);
            } else {
                infoTree = stuInfoMapper.getInfoTree(ROOT);
            }
            if (infoTree == null) {
                throw new DAOException("infoTreeNode not exist");
            }
        } catch (DAOException e) {
            log.warn("QUERY [infoTree] FAIL\nINPUT OBJECT: {}\nREASON: {}", code, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "查询学籍信息树失败\n原因: 学籍信息不存在");
        } catch (Exception e) {
            log.error("QUERY [infoTree] FAIL\nINPUT PARAM: {}\nREASON: {}", code, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询失败");

        }
        return infoTree;
    }

    @Override
    public PageInfo<StuInfoDTO> pageStuInfos(PageDTO page, StuInfoDTO stuInfoDTO) {
        try {
            PageHelper.startPage(page);
            List<StuInfoDTO> stuInfoList = stuInfoMapper.listStuInfos(stuInfoDTO);
            return new PageInfo<>(stuInfoList);
        } catch (Exception e) {
            log.error("PAGE [stuInfo] FAIL\nINPUT OBJECT: {}\nREASON: {}", stuInfoDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询学籍信息分页列表失败");
        }
    }

    @Override
    public List<StuInfoDTO> listStuInfos(StuInfoDTO stuInfoDTO) {
        try {
            return stuInfoMapper.listStuInfos(stuInfoDTO);
        } catch (Exception e) {
            log.error("LIST [stuInfo] FAIL\nINPUT OBJECT: {}\nREASON: {}", stuInfoDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询学籍信息列表失败");
        }
    }

    @Override
    public int addStuInfo(StuInfoDTO stuInfoDTO) {
        StuInfo stuInfo = new StuInfo();
        BeanUtils.copyProperties(stuInfoDTO, stuInfo);

        int status;
        try {
            status = stuInfoMapper.addStuInfo(stuInfo);
        } catch (DuplicateKeyException e) {
            log.warn("ADD [stuInfo] FAIL\nINPUT OBJECT: {}\nREASON: {}", stuInfoDTO, e.toString());
            throw new ResultException(ResultEnum.DUPLICATE_KEY_CONFLICT, "添加学籍信息失败\n原因: 学籍信息id已存在");
        } catch (Exception e) {
            log.error("ADD [stuInfo] FAIL\nINPUT OBJECT: {}\nREASON: {}", stuInfoDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "添加学籍信息失败");
        }
        return status;
    }

    @Override
    public int updateStuInfo(StuInfoDTO stuInfoDTO) {
        StuInfo stuInfo = new StuInfo();
        BeanUtils.copyProperties(stuInfoDTO, stuInfo);

        int status;
        try {
            status = stuInfoMapper.updateStuInfo(stuInfo);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [stuInfo] record not exist");
            }
        } catch (DAOException e) {
            log.warn("UPDATE [stuInfo] FAIL\nINPUT OBJECT: {}\nREASON: {}", stuInfoDTO, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "更新学籍信息失败\n原因: 学籍信息不存在");
        } catch (Exception e) {
            log.error("UPDATE [stuInfo] FAIL\nINPUT OBJECT: {}\nREASON: {}", stuInfoDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "更新学籍信息失败");
        }
        return status;
    }

    @Override
    public int deleteStuInfoById(Integer id) {
        int status;
        try {
            status = stuInfoMapper.deleteStuInfoById(id);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [stuInfo] record not exist");
            }
        } catch (DAOException e) {
            log.warn("DELETE [stuInfo] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "删除学籍信息失败\n原因: 学籍信息不存在");
        } catch (Exception e) {
            log.error("DELETE [stuInfo] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.ERROR, "删除学籍信息失败");
        }
        return status;
    }


}



